﻿using AutoMapper;
using BW.WMS.WareHouseCheck.DTO;
using BW.WMS.WareHouseManage;
using BW.WMS.WareHouseStatistics.Page;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace BW.WMS.WareHouseCheck
{
    public class WarehouseService : ApplicationService, IWarehouseService
    {
        private readonly IRepository<StockTaking, Guid> _warehouseRepository;
        private readonly IRepository<Warehouse,Guid> _Repository;

        public WarehouseService(IRepository<StockTaking, Guid> warehouseRepository, IRepository<Warehouse, Guid> repository)
        {
            _warehouseRepository = warehouseRepository;
            _Repository = repository;
        }

        /// <summary>
        /// 添加仓库盘点
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public async Task<WarehouseDTO> CreateWarehouses(EditWarehouseDTO input)
        {
            var house = ObjectMapper.Map<EditWarehouseDTO, StockTaking>(input);

            Random random = new Random();
            house.StockTakingCode = "KCPD" + random.Next(1000, 10000);
            house.StartTime = DateTime.Now;

            await _warehouseRepository.InsertAsync(house);
            return ObjectMapper.Map<StockTaking, WarehouseDTO>(house);
        }

        /// <summary>
        /// 删除仓库盘点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteWarehouses(Guid id)
        {
            await _warehouseRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 获取仓库盘点详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<WarehouseDTO> FindWarehouses(Guid id)
        {
            var warehouse = await _warehouseRepository.GetAsync(id);

            return ObjectMapper.Map<StockTaking, WarehouseDTO>(warehouse);
        }

        /// <summary>
        /// 获取仓库盘点列表
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResultDto<WarehouseDTO>> GetWarehousesList(PageWarehouse page)
        {
            //获取盘点表所有数据
            var house = await _warehouseRepository.GetQueryableAsync();

            //获取仓库表所有数据
            var lists = await _Repository.GetQueryableAsync();

            // 联查
            var query = from stock in house
                        join warehouse in lists on stock.WarehouseId equals warehouse.Id
                        select new WarehouseDTO
                        {
                            Id = stock.Id,
                            StockTakingCode = stock.StockTakingCode,
                            WarehouseId = stock.WarehouseId,
                            StockTakingType = stock.StockTakingType,
                            StartTime = stock.StartTime,
                            EndTime = stock.EndTime,
                            StockTakingGoods = stock.StockTakingGoods,
                            Agent = stock.Agent,
                            Desc = stock.Desc,
                            Name = warehouse.Name // 仓库名称
                        };

            //查询条件过滤
            if (!string.IsNullOrEmpty(page.StockTakingCode)) 
            {
                query = query.Where(x => x.StockTakingCode != null && x.StockTakingCode.Contains(page.StockTakingCode));
            }
            if (!string.IsNullOrEmpty(page.StockTakingType))
            {
                query = query.Where(x => x.StockTakingType != null && x.StockTakingType.Contains(page.StockTakingType));
            }
            if (page.WarehouseId != null)
            {
                query = query.Where(x => x.WarehouseId != null && x.WarehouseId.Equals(page.WarehouseId));
            }

            //分页
            var count = query.Count();
            var list = query.Skip((page.PageIndex - 1) * page.PageSize).Take(page.PageSize).ToList();

            //返回分页结果
            return PagedResultDto<WarehouseDTO>.Create(list, count, page.PageIndex, page.PageSize);
        }

        /// <summary>
        /// 修改仓库盘点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<WarehouseDTO> UpdateWarehouses(Guid id, EditWarehouseDTO input)
        {
            //先根据Id获取数据
            var warehouse = await _warehouseRepository.FirstOrDefaultAsync(x => x.Id == id);

            //若未找到输出错错误信息
            if (warehouse==null) 
            {
                throw new UserFriendlyException("未查询到您要修改的数据");
            }

            //使用AutoMapper将输入参数映射到实体
            ObjectMapper.Map(input, warehouse);

            //更新到数据库
            await _warehouseRepository.UpdateAsync(warehouse);

            //将更新后的实体映射到DTO并返回
            return ObjectMapper.Map<StockTaking, WarehouseDTO>(warehouse);
        }
    }
}
